class ListNode {
    val: number;
    next: ListNode | null;
    constructor(val?: number, next?: ListNode | null) {
        this.val = val === undefined ? 0 : val;
        this.next = next === undefined ? null : next;
    }
}
function swapPairs(head: ListNode | null): ListNode | null {

    // 创建哑节点简化操作
    const dummy = new ListNode(0);
    dummy.next = head;
    let current: ListNode | null = dummy;

    // 当存在两个连续的节点可以交换时
    while (current?.next && current.next.next) {
        const first = current.next;
        const second = current.next.next;

        // 执行交换
        first.next = second.next;
        second.next = first;
        current.next = second;

        // 移动到下一对的前一个节点
        current = first;
    }

    return dummy.next;
};